package b2.widget.tech
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.widget.*;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.filters.*;
	import flash.text.*;

	public class TechTab extends Sprite
	{
		protected var originalSlot:ObjectSlot;
		protected var objectSlots:Array;
		protected var warningTip:TextField;
		protected var startButton:BasicButton;
		protected var successRateText2:TextField;
		protected var numSel:NumberSelecter;
		
		public function TechTab()
		{
			super();
			//原件槽
			originalSlot = new ObjectSlot();
			originalSlot.x = 50;
			originalSlot.y = 100;
			//消耗材料
			objectSlots = new Array();
			for (var i:int = 0; i < 3; i++)
			{
				var objectSlot:ObjectSlot = new ObjectSlot();
				objectSlot.x = 35;
				objectSlot.y = 240 + i * 60;
				objectSlots.push(objectSlot);
				addChild(this.objectSlots[i]);
			}
			//所需资源
			warningTip = new TextField();
			warningTip.selectable = false;
        	warningTip.autoSize = TextFieldAutoSize.CENTER;
			warningTip.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFF0000, true);
			warningTip.x = 180;
			warningTip.y = 150;
			addChild(warningTip);
			//所需资源
			var resourceText:TextField = new TextField();
			resourceText.selectable = false;
        	resourceText.autoSize = TextFieldAutoSize.LEFT;
			resourceText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			resourceText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			resourceText.text = "所需资源";
			resourceText.x = 65;
			resourceText.y = 198;
			addChild(resourceText);
			//催化剂
			var activatorText:TextField = new TextField();
			activatorText.selectable = false;
        	activatorText.autoSize = TextFieldAutoSize.LEFT;
			activatorText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			activatorText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			activatorText.text = "催化剂";
			activatorText.x = 238;
			activatorText.y = 198;
			addChild(activatorText);
			//成功率
			var successRateText:TextField = new TextField();
			successRateText.selectable = false;
        	successRateText.autoSize = TextFieldAutoSize.LEFT;
			successRateText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			successRateText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			successRateText.text = "成功率";
			successRateText.x = 203;
			successRateText.y = 245;
			addChild(successRateText);
			//成功率2
			successRateText2 = new TextField();
			successRateText2.selectable = false;
        	successRateText2.autoSize = TextFieldAutoSize.CENTER;
			successRateText2.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			successRateText2.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			successRateText2.x = 283;
			successRateText2.y = 245;
			addChild(successRateText2);
			//催化剂2
			var activatorText2:TextField = new TextField();
			activatorText2.selectable = false;
        	activatorText2.autoSize = TextFieldAutoSize.LEFT;
			activatorText2.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			activatorText2.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			activatorText2.text = "催化剂";
			activatorText2.x = 203;
			activatorText2.y = 280;
			addChild(activatorText2);
			//催化剂图标
			var objectItem:ObjectItem = new ObjectItem(CommonObjConfig.ACT, 0, 0, 0, 0, -1, false);
			objectItem.x = 263;
			objectItem.y = 280;
			addChild(objectItem);
			//加载数量选择器
			var LeftArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			var RightArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			numSel = new NumberSelecter(LeftArrowButton, RightArrowButton);
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var activatorNum:int = me.getObjNum(CommonObjConfig.ACT, 0, 0);
			numSel = new NumberSelecter(LeftArrowButton, RightArrowButton, 0, 0, activatorNum);
			numSel.setNumChangeCallback(onActivatorNumChange);
			numSel.x = 218;
			numSel.y = 340;
			addChild(numSel);
		}
		
		private function onActivatorNumChange():void
		{
			refreshSuccessRate();
		}
		
		protected function refreshSuccessRate():void
		{
			if (!originalSlot.hasObj)
				return;
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var activatorNum:int = me.getObjNum(CommonObjConfig.ACT, 0, 0);
			numSel.max = activatorNum;
			var objectConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
			if (!objectConfig.noBasicLevel)
			{
				var basicLevel:int = originalSlot.basicLevel;
				var strengthenLevel:int;
				if (objectConfig.noStrengthenLevel)
					strengthenLevel = 4;
				else
					strengthenLevel = originalSlot.strengthenLevel;
				var successRate:int = parseInt(objectConfig.successRate[basicLevel * 5 + strengthenLevel]) + Config.basicConfig.ratePerActivator * numSel.value;
				successRateText2.text = successRate + "%";
			}
		}
		
		public function loadMaterial():void
		{
			if (!originalSlot.hasObj)
				return;
			var objectConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
			var basicLevel:int = originalSlot.basicLevel;
			var strengthenLevel:int = originalSlot.strengthenLevel;
			var materialIndex:Array = objectConfig.materialIndex;
			var materialNum:Array = objectConfig.materialNum;
			for (var i:int = 0; i < materialIndex.length; i++)
			{
				if (materialIndex[i] >= 0)
				{
					var materialConfig:ObjectConfig = Config.objectConfigs[materialIndex[i]];
					var bLevel:int = basicLevel;
					var sLevel:int = strengthenLevel;
					if (materialConfig.noBasicLevel)
					{
						bLevel = 0;
						sLevel = 0;
					}
					else if (materialConfig.noStrengthenLevel)
					{
						bLevel = basicLevel;
						sLevel = 0;
					}
					(objectSlots[i] as ObjectSlot).loadObj(materialIndex[i], bLevel, sLevel, -1, materialNum[i][basicLevel * 5 + strengthenLevel], 
						0, true, true);
				}
			}
			refreshSuccessRate();
			if (!hasEnoughMaterial)
			{
				warningTip.text = "需要的材料不足！";
				startButton.disable = true;
			}
		}
		
		public function generateObjBalance():String
		{
			var objBalance:String = "";
			//需要材料
			for (var i:int = 0; i < objectSlots.length; i++)
			{
				var objectSlot:ObjectSlot = objectSlots[i];
				if (objectSlot.hasObj)
				{
					if (i > 0)
						objBalance += ",";
					objBalance += PlayerInfoManager.getInstance().myUserId + "," + objectSlot.objectIndex + "," + objectSlot.basicLevel + "," + 
						objectSlot.strengthenLevel + "," + objectSlot.tankId + ",-" + objectSlot.num;
				}
			}
			//催化剂
			if (numSel.value > 0)
				objBalance += "," + PlayerInfoManager.getInstance().myUserId + "," + CommonObjConfig.ACT + ",0,0,0,-" + numSel.value;
			return objBalance;
		}
		
		
		public function get hasEnoughMaterial():Boolean
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			for (var i:int = 0; i < objectSlots.length; i++)
			{
				var objectSlot:ObjectSlot = objectSlots[i];
				if (objectSlot.hasObj)
				{
					var ownNum:int = me.objPackage.objectList[objectSlot.comIndex];
					if (ownNum < objectSlot.num)
						return false;
				}
			}
			return true;
		}
		
		public virtual function loadObject(objectIndex:int, basicLevel:int, strengthenLevel:int, tankId:int = 0):void
		{
			
		}
		
		public virtual function removeAll():void
		{
			numSel.value = 0;
			warningTip.text = "";
			successRateText2.text = "";
			for (var i:int = 0; i < objectSlots.length; i++)
			{
				var objectSlot:ObjectSlot = objectSlots[i];
				objectSlot.removeObj();	
			}
			startButton.disable = true;
		}
	}
}